<?php

/*
*/

class Advice_model extends CI_Model
{

	static $TABLE;

	function __construct()
	{
		parent::__construct();
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化
		$this->TABLE = 'advices';	//数据库表名
		$this->mode = FALSE;	//模式
	}

	/***
	For private function here
	***/
	private function make_single_advice($start_date, $end_date, $view=FALSE)
	{
		//导入日期辅助函数
		//导入模型
		$this->load->model('stock_model');
		$this->load->model('stock_detail_model');
		$this->load->model('stock_advices_model');
		$this->load->model('sale_advice_model');
		//载入辅助函数
		$this->load->helper('date');

		//初始化参数
		$stock_details = array();

		//处理日期
		$start_date = mktime(0, 0, 0, substr($start_date, 4), 1, substr($start_date, 0, 4));
		$end_year = substr($end_date, 0, 4);
		$end_month = substr($end_date, 4);
<<<<<<< HEAD
		$end_date = mktime(0, 0, 0, $end_month, 1, $end_year) - 1;
=======
		$end_date = mktime(0, 0, 0, $end_month, 1, $end_year);
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
		$data = array(
			'advice_date'	=> $start_date,
			'create_date'	=> time()
			);

		//判断处理模式
		if ($view)
		{
			//观察模式
		}
		else
		{
			//写入模式
			if ($this->db->insert($this->TABLE, $data))
			{
				$advice_id = $this->db->insert_id();	//新增结算ID
				$advice_date = $start_date;				//结算日期

				//获得指定时间区间的所有库存单详情
				$stock_ids = $this->stock_model->get_stock_ids_by_date($start_date, $end_date, array('del_flg'=>0, 'balance_flg'=>0));
				$stock_details = $this->stock_detail_model->get_detail($stock_ids, 'STOCK_IDS', 'SINGLE');

				foreach ($stock_ids as $stock_id)
				{
					//为出入库单设置结算标识符
					if (!$this->stock_model->set_balanced($stock_id))
					{
						$this->error = '不能设置结算标识符';
						return FALSE;
					}
					//关联结算单ID
					if (!$this->stock_model->relate_advice($stock_id, $advice_id))
					{
						$this->error = '不能关联结算报表';
						return FALSE;
					}
				}

				//更新库存结算报表
				if (FALSE !== $this->stock_advices_model->create_advice($advice_id, $advice_date, $stock_details))
				{
					//更新销售结算报表
					//获得更新价格后的库存单
					$stock_details = $this->stock_detail_model->get_detail($stock_ids, 'STOCK_IDS', 'SINGLE');
					if (FALSE !== $this->sale_advice_model->create_advice($advice_id, $advice_date, $stock_details))
					{
						return TRUE;
					}
					else
					{
						$this->error = $this->sale_advice_model->error;
					}
				}
				else
				{
					$this->error = $this->stock_advices_model->error;
				}
			}
			else
			{
<<<<<<< HEAD
=======
				echo $this->db->last_query();
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
				$this->error = __CLASS__ . '::' . __FUNCTION__ . ' ON LINE ' . __LINE__ . ':暂时无法结算，请稍候再试或联系管理员。';
			}
		}

		return FALSE;

	}
	/***
	For public function here
	***/

	//下列函数提供给视图使用
	//计算需要多少个报表周期
	public function make_advices()
	{
		//导入库存模型
		$this->load->model('stock_model');

		//判断是否已经生成结算单
		$this->db->limit(1);
		$this->db->order_by('advice_id DESC');
		$advices = $this->db->get($this->TABLE)->result();
		$end_date = $this->stock_model->get_stock_date('last');
		if (0 == count($advices))
		{
			//获得第一个周期起始
			$start_date = $this->stock_model->get_stock_date('first');
		}
		else
		{
			foreach ($advices as $advice)
			{
				$start_date = $advice->advice_date;
				break;
			}
		}

<<<<<<< HEAD
=======
		//转化为标准格式日期
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
		$start_date = date('Ym', $start_date);
		$end_date = date('Ym', $end_date);

		//获得相关日期数据
		$start_year = substr($start_date, 0, 4);
		$start_month = substr($start_date, 4);
		$end_year = substr($end_date, 0, 4);
		$end_month = substr($end_date, 4);

		//处理起始日期
		if (count($advices) > 0)
		{
			if ($start_month == 12)
			{
				$start_year += 1;
				$start_month = 1;
			}
<<<<<<< HEAD
=======
			else
			{
				$start_month += 1;
			}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
		}

		//计算需要生成多少个周期的数据
		if ($end_year - $start_year == 0)
		{
			$circle = $end_month - $start_month;
		}
		else if ($end_year - $start_year > 0)
		{
			$circle = 12 - ($start_month - $end_month);
		}
		else
		{
<<<<<<< HEAD
			$this->error = '计算结算周期出错。';
=======
			$this->error = '计算结算周期出错。' . $start_year . '-' . $end_year;
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
			return FALSE;
		}

		//不足一个计算周期
		if ($circle == 0)
		{
<<<<<<< HEAD
=======
			//$this->make_single_advice($start_date, $end_date, 'VIEW');
			//$this->error = '结算不足一个周期';
			//return FALSE;
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
			$circle = 1;
		}

		//根据周期生成结算单
		for ($i = 0; $i < $circle; $i++)
		{
			if ($i > 0)
			{
				if ($start_month == 12)
				{
					$start_year = $start_year + 1;
					$start_month = 1;
				}
				else
				{
					$start_month = $start_month + 1;
				}
			}

<<<<<<< HEAD
=======
			//计算订单结束时间
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
			if ($start_month == 12)
			{
				$end_year = $start_year + 1;
				$end_month = 1;
			}
			else
			{
				$end_year = $start_year;
				$end_month = $start_month + 1;
			}

			$start_date = $start_year . $start_month;
			$end_date = $end_year . $end_month;

			if (FALSE === $this->make_single_advice($start_date, $end_date))
			{
				return FALSE;
			}
		}

	}

	//查看一个周期的库存结算数据
	public function view($type, $args)
	{
		//载入需要的模型
		$this->load->model('stock_advices_model');
		$this->load->model('sale_advice_model');

		//选择查看类型
		switch ($type) {
			case 'stock':
				$date = $args[2];
				//获得库存概要数据
				if (!$advice = $this->stock_advices_model->get_one('DATE', $date))
				{
					$this->error = $this->stock_advices_model->error;
					return FALSE;
				}
				break;
			case 'sale':
				$date = $args[2];
				//获得库存概要数据
				if (!$advice = $this->sale_advice_model->get_one('DATE', $date))
				{
					$this->error = $this->sale_advice_model->error;
					return FALSE;
				}
				break;
			default:
				$this->error = '需要指定一个结算类型！';
				return FALSE;
		}
		return $advice;
	}

	//检查结算单的完整性并报告错误
	public function check_advices()
	{
		$this->load->model('stock_advices_model');
		$this->load->model('sale_advice_model');

		$advices = $this->get_advice_ids('advice_id, advice_date');	//所有的结算单ID
		$stock_advices = $this->stock_advices_model->get_all('advice_id, advice_date');
		$sale_advices = $this->sale_advice_model->get_all('advice_id, advice_date');
		$message = array('stock'	=> array(), 'sale' => array());
		$message_date =  array('stock'	=> $stock_advices, 'sale' => $sale_advices);
		foreach ($advices as $advice)
		{
			//检查库存结算报表
			foreach ($stock_advices as $key=>$stock_advice)
			{
				if ($advice['advice_id'] == $stock_advice['advice_id'])
				{
					if ($advice['advice_date'] != $stock_advice['advice_date'])
					{
						$message['stock'][] = date('Y月m月', $advice['advice_date']) . "库存报表破损";
					}
					else
					{
						unset($message_date['stock'][$key]);
					}
				}
			}

			//检查销售结算报表
			foreach ($sale_advices as $key=>$sale_advice)
			{
				if ($advice['advice_id'] == $sale_advice['advice_id'])
				{
					if ($advice['advice_date'] != $sale_advice['advice_date'])
					{
						$message['sale'][] = date('Y月m月', $advice['advice_date']) . "销售报表破损";
					}
					else
					{
						unset($message_date['sale'][$key]);
					}
				}
			}
		}

		//组织报告
		$report = array();
		foreach ($message as $key=>$msgs)
		{
			foreach ($msgs as $msg)
			{
				$report[] = $msg;
			}
		}
		foreach ($message_date as $key=>$msgs)
		{
			if ('stock' == $key)
			{
				$type = '库存';
			}
			else if ('sale' == $key)
			{
				$type = '销售';
			}
			foreach ($msgs as $msg)
			{
				$report[] = date('Y月m月', $msg['advice_date']) . $type . '报表未生成';
			}
		}

		return $report;

	}

	/**********************
	以下是get方法
	**********************/
	//获得所有的advice_id
	public function get_advice_ids($select)
	{
		$this->db->select($select);
		$query = $this->db->get($this->TABLE);
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return 0;
		}
	}

}